package com.vonchenchen.renderer;

import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

/**
 * listening SurfaceView's lifecycle
 * create and call by native
 * Created by lidechen on 2021/1/16.
 */
public class SurfaceEventWrapper implements Callback {

    private static final String TAG = "SurfaceEventWrapper";

    private long mNativePtr;
    private final Object mRenderLock = new Object();
    private SurfaceView mSurfaceView;
    /**
     * called by native
     * @param ptr
     */
    public void setPtr(long ptr){
        synchronized (mRenderLock){
            mNativePtr = ptr;
        }
    }

    /**
     * called by native
     * @param surfaceView
     */
    public void setSurfaceView(SurfaceView surfaceView){
        Log.i(TAG, "renderer SurfaceEventWrapper setSurfaceView1");
        synchronized (mRenderLock){
            removeView();
            mSurfaceView = surfaceView;
            SurfaceHolder holder = mSurfaceView.getHolder();
            if(holder != null){
                Log.i(TAG, "test SurfaceEventWrapper setSurfaceView2");
                holder.addCallback(this);
            }
        }
    }

    public void removeView(){
        Log.i(TAG, "renderer SurfaceEventWrapper removeView");
        synchronized (mRenderLock){
            if(mSurfaceView != null){
                SurfaceHolder holder = mSurfaceView.getHolder();
                if(holder != null){
                    Log.i(TAG, "test SurfaceEventWrapper removeView2");
                    holder.removeCallback(this);
                }
            }
            mSurfaceView = null;
        }
    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
        Log.i(TAG, "renderer SurfaceEventWrapper surfaceCreated");
        synchronized (mRenderLock){
            if(mNativePtr != 0){
                surfaceCreated(mNativePtr, surfaceHolder);
            }
        }
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {
        //Log.i(TAG, "renderer SurfaceEventWrapper surfaceChanged");

        synchronized (mRenderLock) {
            if (mNativePtr != 0) {
                surfaceChanged(mNativePtr, surfaceHolder, i, i1, i2);
            }
        }
    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {
        Log.i(TAG, "renderer SurfaceEventWrapper surfaceDestroyed");
        synchronized (mRenderLock) {
            if (mNativePtr != 0) {
                surfaceDestroyed(mNativePtr, surfaceHolder);
                //egl mtrack???
            }
        }
        //surfaceHolder.getSurface().release();
    }

    private static native void surfaceCreated(long nativePtr, SurfaceHolder surfaceHolder);

    private static native void surfaceChanged(long nativePtr, SurfaceHolder surfaceHolder, int i, int i1, int i2);

    private static native void surfaceDestroyed(long nativePtr, SurfaceHolder surfaceHolder);
}
